

package com.shane.air.makeStoredObject
{
	import flash.events.MouseEvent;
	import flash.display.Sprite;

	public class makeStoredObject extends Sprite
	{	
		import flash.events.*;
		import flash.media.Sound;
		import flash.net.URLRequest;
		import flash.utils.getTimer;
		import com.shane.air.MakeDirectoryList.MakeDirectoryList;
		
		import flash.net.*;
		import flash.filesystem.*;
		import flash.utils.ByteArray;
		var numOfSongs:int;
		var songArr:Array = new Array();
		var songObj:Object;
		var artistsDir:Array = new Array();
		var albumsDir:Array = new Array();
		var albums:Array = new Array();
		var albumsPaths:Array = new Array();
		var songsDir:Array = new Array();
		var songsPaths:Array= new Array();
		var genres:Array = new Array();
		
		var artists:Array = new Array();
		var songNames:Array= new Array();
		var years:Array = new Array();
		
		var makeDirObj:MakeDirectoryList = new MakeDirectoryList();
		var anAlbum:Array; //working array
		var genreInt:int;
		var time:Number = getTimer(); // for speed tests
		var sound:Sound;
		var docDir:File = File.documentsDirectory;
		var dataFile:File = new File(); 
		var dataObj:FileReference = new FileReference(); 
		var writeStream:FileStream = new FileStream();
		var outsideDat:Object = new Object;
		
		public function makeStoredObject() {
		time = getTimer(); // start speed test
		
		dataFile = docDir.resolvePath("re3MusicData/songObjDat.txt");

		//readStream.open(dataFile, FileMode.READ);
		//outsideDat = readStream.readObject();

		//readStream.close();
		//trace("initTime: ", (getTimer()-time));
		//trace("outsideDat.genre: "+outsideDat.genres);
		
			
			
			
			artistsDir = makeDirObj.init("Music");
			var l:int = 0;
			var j:int = 0;
			for(var i:int = 0; i < artistsDir.length; i++) {
				if(artistsDir[i].isDirectory) {
					//out.appendText(artistsDir[i].name + "\n");
					var aArtist:Array = makeDirObj.init("Music/" + artistsDir[i].name);
					j = 0;
					for (j; j < aArtist.length; j++) {
						if(aArtist[j].isDirectory) {
							albumsDir.push(aArtist[j]);	
							
							anAlbum = new Array();
							anAlbum = makeDirObj.init("Music/" + artistsDir[i].name +"/" + aArtist[j].name);
							l = 0;
							for (l; l < anAlbum.length; l++) {
								var filename:String = String(anAlbum[l].name);
								
								if(filename.substr(filename.length - 4, 4) == ".mp3") {
									songsDir.push(anAlbum[l]);
									albumsPaths.push(docDir.url+"/Music/" + artistsDir[i].name +"/" + aArtist[j].name);
									songsPaths.push(docDir.url+"/Music/" + artistsDir[i].name +"/" + aArtist[j].name+"/"+anAlbum[l].name);
								}
							}
						}	
					}
				}
			}
			
			trace(songsPaths);
			
			trace("started data collection");
			numOfSongs = songsDir.length;
			time = getTimer(); // start speed test
			trace("alphabetical sortTime: ", (getTimer()-time));
			//trace(albumsDir);
						 //  out.appendText("\n");
			
			
			sortGenre();
		}
		
		private function sortGenre ():void {
			time = getTimer(); // start speed test
			genres = new Array();
			
			
			genreInt = -1;
			cycleSongs();
			
		}
		private function cycleSongs() :void {
			genreInt++;
			if (genreInt < numOfSongs) {
				sound = null;
				try {
				sound = new Sound(new URLRequest(songsPaths[genreInt]));
				} catch (error:Error) {
					trace("bad song path");
				}
				sound.addEventListener(Event.ID3, onID3, false, 0, true);
			} else {
				
				trace("genreSortTime: ", (getTimer()-time));
				trace("writing data--leave it be");
				for(var i:int = 0; i < numOfSongs; i++) {
					songObj = new Object();
					songObj.genre = genres[i];
					songObj.artist = artists[i];
					
					songObj.songName = songNames[i];
					songObj.year = years[i];
					songObj.album = albums[i];
					songObj.songPath = songsPaths[i];
					songObj.albumPath = albumsPaths[i];
					songObj.artistDir = artistsDir[i];
					songArr.push(songObj);
				}
				outsideDat.songArr = songArr;
				writeStream.open(dataFile, FileMode.WRITE);
				writeStream.writeObject(outsideDat);
				writeStream.close();
				dispatchEvent( new Event("finished"));
				trace("write Finished")
			}
		}
		private function onID3 (e:Event):void {
				//out.appendText(e.currentTarget.id3.artist+ "\n");
				genres.push(sound.id3.genre);
				artists.push(sound.id3.artist);
				trace(sound.id3.artist);
				var snForceLength:String = sound.id3.songName;
				if (snForceLength.length > 26)
					snForceLength = snForceLength.substr(0,26);
				songNames.push(snForceLength);
				albums.push(sound.id3.album);
				years.push(sound.id3.year);
				sound.removeEventListener(Event.ID3, onID3);
				sound = null;
				cycleSongs();
				
		}
	}
}